package battleship;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

public class LobbyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // servlet for when an existing user returns (I think)
        System.out.println("LobbyServlet: starting");
    	String userToken= request.getParameter("token");
    	//System.out.println(userToken);
try {
	int id= (Integer) UserIdTokenHashMap.idTokenMap.get(userToken);
    // int id = Integer.parseInt(request.getParameter("id"));
     User user = Context.users[id];
     if (!user.online) {
         user.online = true;
         Context.onlineUsers.add(user);
         user.lastSeen = new Date();
         user.checkIfOnline(); // start periodical check to see if user is still online
     }

} catch (Exception e) {
	// TODO: handle exception
}

        System.out.println("LobbyServlet: finished");
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/lobby.jsp");
        rd.forward(request, response);
    }
}
